#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.IO;
using System.Collections.Generic;
using EngineOfEvermore.Common;
using NUnit.Framework;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Scene.ObjectLibrary;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Scene.Physics;
using Blue.ResourceManager;
using Green.Logging;
	
namespace EngineOfEvermore.Scene.Test
{
	#if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
	[TestFixture]
	public class SceneGraphSerialization
	{
		[SetUp]
		public void SetUp()
		{
			ResourceManager.Initialize( new FileSystemResourceManager( "Data" ) );
			Scene.Initialization.Init();
			EngineOfEvermore.Common.Initialization.Init();
			Imaging.Initialization.Init();
			new LogTextWriter( System.Console.Out );
			
			XmlSerialization.AddSchemaPath( "../../../../Common/Data/XmlSchema" );
			XmlSerialization.AddSchemaPath( "../../../Data/XmlSchema" );

		}
		
		[TearDown]
		public void TearDown()
		{
			ResourceManager.Dispose();		
		}
		
		
		
		private const string SCENEGRAPH_RESOURCE_NAME = "TestSceneGraph.xscene";
		private const string SCENEOBJECT_DEFINITION_NAME = "TestSceneObjectDefinition.def";

		
		private SceneObject createSceneObject_1()
		{
			SceneObject so1 = new SceneObject();
			so1.Position = new Vector2d(11.1f, 22.2f);
			so1.LinearVelocity = new Vector2d(33.3f, 44.4f);
			so1.Rotation = 1.23f;
			so1.Space = 2;
			
			VisualComponent vc = new VisualComponent();
			vc.PositionOffset = new Vector2d(5.5f, 6.6f);
			vc.RotationOffset = 7.7f;

			CollisionComponent cc = new CollisionComponent();
			cc.Weight = 88.8f;
			System.Drawing.RectangleF rect = new System.Drawing.RectangleF( 2f, 3f, 43.5f, 65.23f );
			RoundedRectangleShape rrs = new RoundedRectangleShape( rect, 5.5f );
			rrs.CollisionGroup = "my group";
			cc.Shapes.Add( rrs );
			
			so1.AddComponent(vc);
			so1.AddComponent(cc);
			
			return so1;
		}
		
		
		internal class TestServiceProvider : IServiceProvider
		{
			public object GetService(Type serviceType) {
				return null;
			}
		}
		
		
		
		[Test]
		public void TestSeneGraphSerialization()
		{
			SceneObject so1 = createSceneObject_1();
			SceneObject so2 = createSceneObject_1();
			
			so1.Position = new Vector2d( 2, 3 );
			so2.Position = new Vector2d( 4, 5 );
			
			SceneGraph g = new SceneGraph();
			g.OnInit( new TestServiceProvider() );
			g.AddSceneObject( so1 );
			g.AddSceneObject( so2 );

			SceneGraphContentResource.SaveContentAndReplace( SCENEGRAPH_RESOURCE_NAME, g );
			
			g = new SceneGraph();
			g.OnInit( new TestServiceProvider() );
			SceneGraphContentResource content = ResourceManager.GetResource<SceneGraphContentResource> ( SCENEGRAPH_RESOURCE_NAME );
			content.AddContentToSceneGraph( g );
			ResourceManager.DeleteResource( SCENEGRAPH_RESOURCE_NAME );
			
			IEnumerator<SceneObject> it = g.SceneObjects.GetEnumerator();
			
			it.MoveNext();
			Assert.AreEqual( so1.Position, it.Current.Position );
			Assert.AreEqual( so1.LinearVelocity, it.Current.LinearVelocity );
			
			it.MoveNext();
			Assert.AreEqual( so2.Position, it.Current.Position );
			Assert.AreEqual( so2.LinearVelocity, it.Current.LinearVelocity );
			
			Assert.IsFalse( it.MoveNext() );
		}
		
		
		[Test]
		public void TestSceneGraphSerializationWithSceneObjectDefinition()
		{
			SceneObject soDef = createSceneObject_1();	
			SceneObjectDefinition.SaveResourceAndReplace( SCENEOBJECT_DEFINITION_NAME, soDef );
			
			SceneObject so1 = createSceneObject_1();
			SceneObject so2 = createSceneObject_1();
			
			so1.AddComponent( new SceneObjectDefinitionRefComponent( SCENEOBJECT_DEFINITION_NAME ) );
			so2.AddComponent( new SceneObjectDefinitionRefComponent( SCENEOBJECT_DEFINITION_NAME ) );
			
			so1.Position = new Vector2d( 2, 3 );
			so1.LinearVelocity = new Vector2d( 4, 5 );
			so1.Rotation = 6;
			so1.Space = 7;
			so2.Position = new Vector2d( 8, 9 );
			so2.LinearVelocity = new Vector2d( 10, 11 );
			so2.Rotation = 12;
			so2.Space = 13;
			
			SceneGraph g = new SceneGraph();
			g.OnInit( new TestServiceProvider() );
			g.AddSceneObject( so1 );
			g.AddSceneObject( so2 );
			SceneGraphContentResource.SaveContentAndReplace( SCENEGRAPH_RESOURCE_NAME, g );
			
			g = new SceneGraph();
			g.OnInit( new TestServiceProvider() );
			SceneGraphContentResource content = ResourceManager.GetResource<SceneGraphContentResource>( SCENEGRAPH_RESOURCE_NAME );
			content.AddContentToSceneGraph( g );
			ResourceManager.DeleteResource( SCENEGRAPH_RESOURCE_NAME );
			ResourceManager.DeleteResource( SCENEOBJECT_DEFINITION_NAME );

			IEnumerator<SceneObject> it = g.SceneObjects.GetEnumerator();
			
			it.MoveNext();
			Assert.AreEqual( so1.Position, it.Current.Position );
			Assert.AreEqual( so1.LinearVelocity, it.Current.LinearVelocity );
			Assert.IsNotNull( it.Current.GetComponent<CollisionComponent>() );
			Assert.IsNotNull( it.Current.GetComponent<VisualComponent>() );
			
			it.MoveNext();
			Assert.AreEqual( so2.Position, it.Current.Position );
			Assert.AreEqual( so2.LinearVelocity, it.Current.LinearVelocity );
			Assert.IsNotNull( it.Current.GetComponent<CollisionComponent>() );
			Assert.IsNotNull( it.Current.GetComponent<VisualComponent>() );
			
			Assert.IsFalse( it.MoveNext() );
			
		}
	}
    #endif
}
